package com.hengqin.macao.activity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.citybrain.rnapp.R;
import com.uuzuche.lib_zxing.activity.CaptureFragment;
import com.uuzuche.lib_zxing.activity.CodeUtils;

public class CameraScanActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //        getSupportActionBar().hide();
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        setContentView(R.layout.activity_qr);//

        checkCamerPermission();
        init();
    }

    private void init(){
        //        ImageView back = findViewById(R.id.bt_back);
        //        back.setOnClickListener(new View.OnClickListener() {
        //            @Override
        //            public void onClick(View v) {
        //                CameraScanActivity.this.finish();
        //            }
        //        });

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 15 && grantResults.length > 0) {
            // 授权成功
            Log.d("bug", "onCreate: "+1);
            CaptureFragment captureFragment = new CaptureFragment();
            Log.d("bug", "onCreate: "+2);
            CodeUtils.setFragmentArgs(captureFragment,-1);  //设置自定义扫码界面
            captureFragment.setAnalyzeCallback(analyzeCallback);
            Log.d("bug", "onCreate: "+3);
            //R.id.fl_zxing_container  对应   setContentView 布局中的  Fragment
            getSupportFragmentManager().beginTransaction().replace(R.id.myCamera, captureFragment).commit();  // 替换setContenView设置的布局中的  ID为myCamera

        }
        else {
            // 授权是吧
            this.finish();
        }
    }

    private void checkCamerPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            if (ActivityCompat.checkSelfPermission(this,
                    android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(this,
                    android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                    || ActivityCompat.checkSelfPermission(this,
                    android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                // 弹出对话框接收权限
                ActivityCompat.requestPermissions(this, new String[] {
                        android.Manifest.permission.CAMERA, android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        android.Manifest.permission.READ_EXTERNAL_STORAGE
                },15);
            }else{
                Log.d("bug", "onCreate: "+1);
                CaptureFragment captureFragment = new CaptureFragment();
                Log.d("bug", "onCreate: "+2);
                CodeUtils.setFragmentArgs(captureFragment,-1);  //设置自定义扫码界面
                captureFragment.setAnalyzeCallback(analyzeCallback);
                Log.d("bug", "onCreate: "+3);
                //R.id.fl_zxing_container  对应   setContentView 布局中的  Fragment
                getSupportFragmentManager().beginTransaction().replace(R.id.myCamera, captureFragment).commit();  // 替换setContenView设置的布局中的  ID为myCamera

            }
        }else{
            Log.d("bug", "onCreate: "+1);
            CaptureFragment captureFragment = new CaptureFragment();
            Log.d("bug", "onCreate: "+2);
            CodeUtils.setFragmentArgs(captureFragment,-1);  //设置自定义扫码界面
            captureFragment.setAnalyzeCallback(analyzeCallback);
            Log.d("bug", "onCreate: "+3);
            //R.id.fl_zxing_container  对应   setContentView 布局中的  Fragment
            getSupportFragmentManager().beginTransaction().replace(R.id.myCamera, captureFragment).commit();  // 替换setContenView设置的布局中的  ID为myCamera

        }
    }

    CodeUtils.AnalyzeCallback analyzeCallback = new CodeUtils.AnalyzeCallback() {
        @Override
        public void onAnalyzeSuccess(Bitmap mBitmap, String result) {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_SUCCESS);
            bundle.putString(CodeUtils.RESULT_STRING, result);
            resultIntent.putExtras(bundle);
            CameraScanActivity.this.setResult(RESULT_OK, resultIntent);
            CameraScanActivity.this.finish();
        }

        @Override
        public void onAnalyzeFailed() {
            Intent resultIntent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putInt(CodeUtils.RESULT_TYPE, CodeUtils.RESULT_FAILED);
            bundle.putString(CodeUtils.RESULT_STRING, "");
            resultIntent.putExtras(bundle);
            CameraScanActivity.this.setResult(RESULT_OK, resultIntent);
            CameraScanActivity.this.finish();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
    }






}

